const util = require('../../utils/util.js')
const getApps = getApp().globalData
const Monitor = require('../../utils/monitor.js').Monitor

Page(Monitor.hookApp({

  data: {
    waysArr:[],// 配送方式的数组
    lists:[],
    imgUrl: getApps.imgUrl,
    imgUrls: getApps.imgUrls,
    current: 1,
    iamgeArr: [1, 2.3, 2],
    showSpecification: false,
    timeList: [],
    skuTypeList: [],
    peiLIst: ['商家配送', '到店自提', '同城限时达'],
    showDistribution: false,
    right: '038b5201805161046447142.png',
    rightImage: "wxshop2854jh5sdf45左边.svg"
  },
  onLoad: function (options) {
    this.setData({
      goods_id: options.goods_id || options.jump_id,
      parent_id: options.parent_id || '0'  // 为0 时候不是分销的的订单
    })
    this.goodsInfo()
    this.getShopList()
  },
  onShow: function () {
    // this.setData({
    //   waysArr: []
    // })
    this.getCardBag()
    // this.addBrows(1, 0, this.data.goods_id)
  },
  previewImage: function(e) {
    var current = e.target.dataset.src;
    var that = this
    wx.previewImage({
      current: that.data.imgUrls + current, // 当前显示图片的http链接
      urls: getimg()
    })

    function getimg() {
      var arrimage = []
      that.data.data.goods_cover.map((item, index) => {
        arrimage.push(that.data.imgUrls + item)
      })
      return arrimage
    }
  },
  showDistribution() {
    this.setData({
      showDistribution: !this.data.showDistribution
    })
  },
  
  jumpDetails(e) {
    wx.redirectTo({
      url: '/pages/shopDetails/shopDetails?goods_id=' + e.currentTarget.dataset.item.goods_id
    })
  },
  getShopList() {
    that = this;
    util.API('spwx/v1.Shop/continueLookLook', {
      goods_id:this.data.goods_id
    
    }).then(res => {
      let {
        code,
        data
      } = res
  
      if (code == 200) {
        that.setData({
          lists: res.data.list,
        })
      } 

    })
  },
  goodsInfo() {
    var that = this
    var {
      goods_id,
      up_user,
      down_user
    } = this.data
    util.API('spwx/v1.Shop/goodsinfo', {
      goods_id,
      up_user,
      down_user
    }).then(res => {
      var {
        data,
        code
      } = res
      if (code === 200) {
        this.data.shop_goods_id = data.goods_id
        data.sku_type = JSON.parse(data.sku_type)
        data.goods_cover = data.goods_cover.split(",");
        data.defaultIndex = data.defaultIndex
        data.goods_content = data.goods_content.replace(/\<img/gi, '<img style="width:100%;height:100%;display:block;margin-top:-1px" ')
        var placeTypes  = [];
        data.sku_type.forEach((items, indexs) => {
          placeTypes.push(items.type)
        })
        data.isVip = data.isVip
        data.distribution_name = JSON.stringify(data.distribution_name)
        let waysArr = this.setWaysArr(this.data.waysArr, data.distribution_type, data.distribution_name)
        this.setData({
          distribution_name: JSON.parse(data.distribution_name),
          placeTypes,
          data,
          waysArr
        })
        // util.intervalTime(1562211922, this,1000)
      }else{
        wx.showToast({
          title: '该商品已经下架',
          icon: 'none',
          success(){
            setTimeout(()=>{
              wx.navigateBack({
                delta: 1,
              })
            },1500)
          }
        })
      }
    })
  },
  setWaysArr(waysArr, distribution_type, distribution_name){  // 设置自定义配送信息
    distribution_type = distribution_type.split(',');
    distribution_name = JSON.parse(distribution_name)
    let len = distribution_type.length
    for(let i = 0; i < len ; i ++){
      waysArr.push(distribution_name[distribution_type[i]])
    }
    return waysArr
  },
  showSpecification() {
    this.setData({
      showSpecification: !this.data.showSpecification
    })
  },
  getCardBag(){
    util.API('spwx/v1.Vipcard/cardBag').then(res => {
      console.log(res.data)
      let {
        noRecieveCards,
        alreadyRecieveCards
      } = res.data
      this.setData({
        noRecieveCards: noRecieveCards,
        alreadyRecieveCards: alreadyRecieveCards
      })
      
    })
  },
  jumpVip(e){
    let card_id = e.currentTarget.dataset.id
    let flag = e.currentTarget.dataset.flag

    wx.navigateTo({
      url: `../Vip/Vip?card_id=${card_id}&index=0&stasus=${flag}`,
    })
  },
  addBrows(type, type_id, goods_id){ // 增加商品浏览记录
    util.API('sp/v1.Assemble/addBrows',{
      type, //1普通 2 拼团 3折扣
      type_id,//活动id
      goods_id//活动id
    })
     // this.addBrows(1, 0, this.data.goods_id)
  },
  onReady: function() {},
  preventTouchMove: function(event) {
    return false
  },
  
  myevent(e) {
    this.setData({
      showSpecification: e.detail.showSpecification
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    clearInterval(this.data.interval);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
 

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function() {
  // },
  bindchange(e) {
    console.log()
    this.setData({
      current: e.detail.current + 1
    })
  }
}))